/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core.api;

import core.api.object.Register;
import core.definition.GlobalConstant;
import core.log.Monitor;
import core.model.UserInfor;

/**
 *
 * @author ASUS_2
 */
public class APIRequest {

    private static final APIUrl APIU = new APIUrl();

    public static UserInfor doLoginAPI(String userName, String pass) {
        UserInfor userInfor = new UserInfor(-1);
        try {
            // url
            String url = APIU.getUrl(APIConstant.AUTH_URL) + APIConstant.LOGIN_METHOD;
            //xu ly param
            String userParam = APIUtility.generateParam(APIConstant.USER_NAME, userName);
            String passParam = APIUtility.generateParam(APIConstant.PASSWORD, pass);
            String salt = userName + System.currentTimeMillis();
            String saltParam = APIUtility.generateParam(APIConstant.SALT, salt);
            String token = APIUtility.getToken(userName, pass, salt);
            String tokenParam = APIUtility.generateParam(APIConstant.TOKEN, token);
            //goi api
            String responseString = API.request(url, userParam, passParam, saltParam, tokenParam);
            // xu ly ket qua tra ve 
            if (responseString != null && !"".equals(responseString)) {
                APIResult result = GlobalConstant.GSON.fromJson(responseString, APIResult.class);
                if (result != null) {
                    userInfor = result.getData(UserInfor.class);
                    userInfor.setCode(result.getCode());
                }
            }
        } catch (Exception e) {
            Monitor.error("doLoginAPI error", e);
            userInfor.setUserID(-1);
        } finally {
            return userInfor;
        }
    }

    public static Register doRegisterAPI(String userName, String pass, String clientInfor, String app, int provider, String email, String phone) {
        Register register = new Register();
        try {
            // url
            String url = APIU.getUrl(APIConstant.AUTH_URL) + APIConstant.REG_METHOD;
            // app key
            String appKey = APIConfig.getInstance().getAPIAppKey();// dung file thu vien property config de doc tu file
            //xu ly param
            String userParam = APIUtility.generateParam(APIConstant.USER_NAME, userName);
            String passParam = APIUtility.generateParam(APIConstant.PASSWORD, pass);
            String clientParam = APIUtility.generateParam(APIConstant.CLIENT_INFOR, clientInfor);
            String appParam = APIUtility.generateParam(APIConstant.APP, app);
            String providerParam = APIUtility.generateParam(APIConstant.PROVIDER, String.valueOf(provider));
            String emailParam = APIUtility.generateParam(APIConstant.EMAIL, email);
            String phoneParam = APIUtility.generateParam(APIConstant.PHONE, phone);
            String salt = userName + provider + System.currentTimeMillis();
            String saltParam = APIUtility.generateParam(APIConstant.SALT, salt);
            String token = APIUtility.getToken(appKey, userName, pass, clientInfor, app, provider, email, phone, salt);
            String tokenParam = APIUtility.generateParam(APIConstant.TOKEN, token);
            // goi API
            String responseString = API.request(url, userParam, passParam, clientParam, appParam, providerParam, emailParam, phoneParam, saltParam, tokenParam);
            // xu ly ket qua tra ve
            if (responseString != null && !"".equals(responseString)) {
                APIResult result = GlobalConstant.GSON.fromJson(responseString, APIResult.class);
                if (result != null) {
                    register = result.getData(Register.class);
                    register.setCode(result.getCode());
                }
            }
        } catch (Exception e) {
            Monitor.error("doRegisterAPI error", e);
            register.setCode(11006);
        } finally {
            return register;
        }
    }
}
